<?php
class PruebasController extends AppController {

	var $name = 'Pruebas';
	var $helpers = array('Access', 'Prueba');

	function sesion_activa(){
		$usuario_activo=$this->Access->getMy('username');
		//Verificacion de sesion del sistema
		if(!$usuario_activo){
			$this->Session->setFlash(__('Por favor inicie sesion', true));
			$this->redirect(array('controller'=>'users','action' => 'login'));
		}
	}
	
	function index() {
		$this->sesion_activa();
		$this->Prueba->recursive = 0;
		$this->set('pruebas', $this->paginate());
	}

	function view($id = null) {
		$this->sesion_activa();
		if (!$id) {
			$this->Session->setFlash(__('Invalid prueba', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('prueba', $this->Prueba->read(null, $id));
	}

	function add($participante = null) {
		$this->sesion_activa();
		if (!empty($this->data)) {
			$this->Prueba->create();
			if ($this->Prueba->save($this->data)) {
				$this->Session->setFlash(__('Prueba Guardada Exitosamente', true));
				$this->redirect(array('controller'=>'participantes','action' => 'index',$this->data['Prueba']['grupo_id']));
			} else {
				$this->Session->setFlash(__('The prueba could not be saved. Please, try again.', true));
			}
		}
		$datos = $this->Prueba->Participante->find('first',array('conditions'=>array('Participante.id'=>$participante)));
		$this->set(compact('datos','participante'));		
	}

	function edit($participante = null) {
		$this->sesion_activa();
		if (!$participante && empty($this->data)) {
			$this->Session->setFlash(__('Invalid prueba', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Prueba->save($this->data)) {				
				$this->Session->setFlash(__('Prueba Guardada Exitosamente', true));
				$this->redirect(array('controller'=>'participantes','action' => 'index',$this->data['Prueba']['grupo_id']));				
			} else {
				$this->Session->setFlash(__('The prueba could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Prueba->Participante->read(null, $participante);
		}
		$datos = $this->data;		
		
		$this->set(compact('datos'));
	}

	function delete($id = null) {
		$this->sesion_activa();
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for prueba', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Prueba->delete($id)) {
			$this->Session->setFlash(__('Prueba deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Prueba was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}